package javolution.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import javolution.JavolutionError;
import javolution.lang.Configurable;
import javolution.lang.MathLib;
import javolution.lang.Reflection;
import net.lingala.zip4j.util.InternalZipConstants;

/* loaded from: classes2.dex */
public class Struct {
    private int _bitIndex;
    private int _bitsUsed;
    private ByteBuffer _byteBuffer;
    private byte[] _bytes;
    private Struct _outer;
    private int _outerOffset;
    public static final Configurable<Integer> MAXIMUM_ALIGNMENT = new Configurable<>(new Integer(4));
    private static final Reflection.Method ADDRESS_METHOD = Reflection.getMethod("sun.nio.ch.DirectBuffer.address()");
    private static final char[] HEXA = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static final Class BOOL = new Bool[0].getClass();
    private static final Class SIGNED_8 = new Signed8[0].getClass();
    private static final Class UNSIGNED_8 = new Unsigned8[0].getClass();
    private static final Class SIGNED_16 = new Signed16[0].getClass();
    private static final Class UNSIGNED_16 = new Unsigned16[0].getClass();
    private static final Class SIGNED_32 = new Signed32[0].getClass();
    private static final Class UNSIGNED_32 = new Unsigned32[0].getClass();
    private static final Class SIGNED_64 = new Signed64[0].getClass();
    private static final Class FLOAT_32 = new Float32[0].getClass();
    private static final Class FLOAT_64 = new Float64[0].getClass();
    private int _alignment = 1;
    private boolean _resetIndex = isUnion();

    /* loaded from: classes2.dex */
    public class Bool extends Member {
        private final int _mask;
        private final int _shift;

        public Bool(Struct struct) {
            this(8);
        }

        public Bool(int i) {
            super(1, i, 8);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (8 - i2) + offset : (i2 - offset) - i;
            this._mask = ((1 << i) - 1) << this._shift;
        }

        public boolean get() {
            return (Struct.this.getByteBuffer().get(position()) & this._mask) != 0;
        }

        public void set(boolean z) {
            ByteBuffer byteBuffer;
            int position;
            if (this._mask == 255) {
                Struct.this.getByteBuffer().put(position(), z ? (byte) 1 : (byte) 0);
                return;
            }
            int i = Struct.this.getByteBuffer().get(position()) & (this._mask ^ (-1));
            if (z) {
                byteBuffer = Struct.this.getByteBuffer();
                position = position();
                i |= 1 << this._shift;
            } else {
                byteBuffer = Struct.this.getByteBuffer();
                position = position();
            }
            byteBuffer.put(position, (byte) i);
        }
    }

    /* loaded from: classes2.dex */
    public class Enum16 extends Member {
        private final List _enumValues;
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Enum16(Struct struct, List list) {
            this(list, 16);
        }

        public Enum16(List list, int i) {
            super(2, i, 16);
            this._enumValues = list;
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (16 - i2) + offset : (i2 - offset) - i;
            int i3 = this._shift;
            this._mask = ((1 << i) - 1) << i3;
            this._signShift = (32 - i3) - i;
        }

        public Enum get() {
            Object obj;
            if (this._mask == 65535) {
                obj = this._enumValues.get(Struct.this.getByteBuffer().getShort(position()));
            } else {
                int i = Struct.this.getByteBuffer().getShort(position()) & this._mask;
                int i2 = this._signShift;
                obj = this._enumValues.get((i << i2) >> (i2 + this._shift));
            }
            return (Enum) obj;
        }

        public void set(Enum r4) {
            int ordinal = r4.ordinal();
            if (this._enumValues.get(ordinal) != r4) {
                throw new IllegalArgumentException("enum: " + r4 + ", ordinal value does not reflect enum values position");
            }
            short s = (short) ordinal;
            int i = this._mask;
            if (i == 65535) {
                Struct.this.getByteBuffer().putShort(position(), s);
                return;
            }
            Struct.this.getByteBuffer().putShort(position(), (short) (((short) (((short) (s << this._shift)) & i)) | (Struct.this.getByteBuffer().getShort(position()) & (this._mask ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class Enum32 extends Member {
        private final List _enumValues;
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Enum32(Struct struct, List list) {
            this(list, 32);
        }

        public Enum32(List list, int i) {
            super(4, i, 32);
            this._enumValues = list;
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (32 - i2) + offset : (i2 - offset) - i;
            this._mask = i == 32 ? -1 : ((1 << i) - 1) << this._shift;
            this._signShift = (32 - this._shift) - i;
        }

        public Enum get() {
            Object obj;
            if (this._mask == -1) {
                obj = this._enumValues.get(Struct.this.getByteBuffer().getInt(position()));
            } else {
                int i = Struct.this.getByteBuffer().getInt(position()) & this._mask;
                int i2 = this._signShift;
                obj = this._enumValues.get((i << i2) >> (i2 + this._shift));
            }
            return (Enum) obj;
        }

        public void set(Enum r4) {
            int ordinal = r4.ordinal();
            if (this._enumValues.get(ordinal) != r4) {
                throw new IllegalArgumentException("enum: " + r4 + ", ordinal value does not reflect enum values position");
            }
            int i = this._mask;
            if (i == -1) {
                Struct.this.getByteBuffer().putInt(position(), ordinal);
                return;
            }
            Struct.this.getByteBuffer().putInt(position(), (i & (ordinal << this._shift)) | (Struct.this.getByteBuffer().getInt(position()) & ((-1) ^ this._mask)));
        }
    }

    /* loaded from: classes2.dex */
    public class Enum64 extends Member {
        private final List _enumValues;
        private final long _mask;
        private final int _shift;
        private final int _signShift;

        public Enum64(Struct struct, List list) {
            this(list, 64);
        }

        public Enum64(List list, int i) {
            super(8, i, 64);
            this._enumValues = list;
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (64 - i2) + offset : (i2 - offset) - i;
            this._mask = i == 64 ? -1L : ((1 << i) - 1) << this._shift;
            this._signShift = (64 - this._shift) - i;
        }

        public Enum get() {
            Object obj;
            if (this._mask == -1) {
                obj = this._enumValues.get((int) Struct.this.getByteBuffer().getLong(position()));
            } else {
                long j = Struct.this.getByteBuffer().getLong(position()) & this._mask;
                int i = this._signShift;
                obj = this._enumValues.get((int) ((j << i) >> (i + this._shift)));
            }
            return (Enum) obj;
        }

        public void set(Enum r9) {
            int ordinal = r9.ordinal();
            if (this._enumValues.get(ordinal) != r9) {
                throw new IllegalArgumentException("enum: " + r9 + ", ordinal value does not reflect enum values position");
            }
            long j = ordinal;
            long j2 = this._mask;
            if (j2 == -1) {
                Struct.this.getByteBuffer().putLong(position(), j);
                return;
            }
            Struct.this.getByteBuffer().putLong(position(), ((j << this._shift) & j2) | (Struct.this.getByteBuffer().getLong(position()) & ((-1) ^ this._mask)));
        }
    }

    /* loaded from: classes2.dex */
    public class Enum8 extends Member {
        private final List _enumValues;
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Enum8(Struct struct, List list) {
            this(list, 8);
        }

        public Enum8(List list, int i) {
            super(1, i, 8);
            this._enumValues = list;
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (8 - i2) + offset : (i2 - offset) - i;
            int i3 = this._shift;
            this._mask = ((1 << i) - 1) << i3;
            this._signShift = (32 - i3) - i;
        }

        public Enum get() {
            Object obj;
            if (this._mask == 255) {
                obj = this._enumValues.get(Struct.this.getByteBuffer().get(position()));
            } else {
                int i = Struct.this.getByteBuffer().get(position()) & this._mask;
                int i2 = this._signShift;
                obj = this._enumValues.get((i << i2) >> (i2 + this._shift));
            }
            return (Enum) obj;
        }

        public void set(Enum r4) {
            int ordinal = r4.ordinal();
            if (this._enumValues.get(ordinal) != r4) {
                throw new IllegalArgumentException("enum: " + r4 + ", ordinal value does not reflect enum values position");
            }
            byte b = (byte) ordinal;
            int i = this._mask;
            if (i == 255) {
                Struct.this.getByteBuffer().put(position(), b);
                return;
            }
            Struct.this.getByteBuffer().put(position(), (byte) (((byte) (((byte) (b << this._shift)) & i)) | (Struct.this.getByteBuffer().get(position()) & (this._mask ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class Float32 extends Member {
        public Float32() {
            super(4, 4);
        }

        public float get() {
            return Struct.this.getByteBuffer().getFloat(position());
        }

        public void set(float f) {
            Struct.this.getByteBuffer().putFloat(position(), f);
        }
    }

    /* loaded from: classes2.dex */
    public class Float64 extends Member {
        public Float64() {
            super(8, 8);
        }

        public double get() {
            return Struct.this.getByteBuffer().getDouble(position());
        }

        public void set(double d) {
            Struct.this.getByteBuffer().putDouble(position(), d);
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    /* loaded from: classes2.dex */
    public class Member {
        private final int _offset;

        protected Member(int i, int i2) {
            int i3 = i2 << 3;
            this._offset = Struct.this.updateIndexes(i, i3, i3);
        }

        Member(int i, int i2, int i3) {
            this._offset = Struct.this.updateIndexes(i, i2, i3);
        }

        public final int offset() {
            return this._offset;
        }

        public final int position() {
            return Struct.this.getByteBufferPosition() + this._offset;
        }

        public final Struct struct() {
            return Struct.this;
        }
    }

    /* loaded from: classes2.dex */
    public class Reference32<S extends Struct> extends Member {
        private S _struct;

        public Reference32() {
            super(4, 4);
        }

        public S get() {
            return this._struct;
        }

        public boolean isUpToDate() {
            return this._struct != null ? Struct.this.getByteBuffer().getInt(position()) == ((int) this._struct.address()) : Struct.this.getByteBuffer().getInt(position()) == 0;
        }

        public void set(S s) {
            ByteBuffer byteBuffer = Struct.this.getByteBuffer();
            int position = position();
            if (s != null) {
                byteBuffer.putInt(position, (int) s.address());
            } else {
                byteBuffer.putInt(position, 0);
            }
            this._struct = s;
        }

        public int value() {
            return Struct.this.getByteBuffer().getInt(position());
        }
    }

    /* loaded from: classes2.dex */
    public class Reference64<S extends Struct> extends Member {
        private S _struct;

        public Reference64() {
            super(8, 8);
        }

        public S get() {
            return this._struct;
        }

        public boolean isUpToDate() {
            return this._struct != null ? Struct.this.getByteBuffer().getLong(position()) == this._struct.address() : Struct.this.getByteBuffer().getLong(position()) == 0;
        }

        public void set(S s) {
            ByteBuffer byteBuffer;
            int position;
            long j;
            if (s == null) {
                if (s == null) {
                    byteBuffer = Struct.this.getByteBuffer();
                    position = position();
                    j = 0;
                }
                this._struct = s;
            }
            byteBuffer = Struct.this.getByteBuffer();
            position = position();
            j = s.address();
            byteBuffer.putLong(position, j);
            this._struct = s;
        }

        public long value() {
            return Struct.this.getByteBuffer().getLong(position());
        }
    }

    /* loaded from: classes2.dex */
    public class Signed16 extends Member {
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Signed16(Struct struct) {
            this(16);
        }

        public Signed16(int i) {
            super(2, i, 16);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (16 - i2) + offset : (i2 - offset) - i;
            int i3 = this._shift;
            this._mask = ((1 << i) - 1) << i3;
            this._signShift = (32 - i3) - i;
        }

        public short get() {
            if (this._mask == 65535) {
                return Struct.this.getByteBuffer().getShort(position());
            }
            int i = Struct.this.getByteBuffer().getShort(position()) & this._mask;
            int i2 = this._signShift;
            return (short) ((i << i2) >> (i2 + this._shift));
        }

        public void set(short s) {
            int i = this._mask;
            if (i == 65535) {
                Struct.this.getByteBuffer().putShort(position(), s);
                return;
            }
            Struct.this.getByteBuffer().putShort(position(), (short) (((short) (((short) (s << this._shift)) & i)) | (Struct.this.getByteBuffer().getShort(position()) & (this._mask ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class Signed32 extends Member {
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Signed32(Struct struct) {
            this(32);
        }

        public Signed32(int i) {
            super(4, i, 32);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (32 - i2) + offset : (i2 - offset) - i;
            this._mask = i == 32 ? -1 : ((1 << i) - 1) << this._shift;
            this._signShift = (32 - this._shift) - i;
        }

        public int get() {
            if (this._mask == -1) {
                return Struct.this.getByteBuffer().getInt(position());
            }
            int i = Struct.this.getByteBuffer().getInt(position()) & this._mask;
            int i2 = this._signShift;
            return (i << i2) >> (i2 + this._shift);
        }

        public void set(int i) {
            int i2 = this._mask;
            if (i2 == -1) {
                Struct.this.getByteBuffer().putInt(position(), i);
                return;
            }
            Struct.this.getByteBuffer().putInt(position(), ((i << this._shift) & i2) | (Struct.this.getByteBuffer().getInt(position()) & ((-1) ^ this._mask)));
        }
    }

    /* loaded from: classes2.dex */
    public class Signed64 extends Member {
        private final long _mask;
        private final int _shift;
        private final int _signShift;

        public Signed64(Struct struct) {
            this(64);
        }

        public Signed64(int i) {
            super(8, i, 64);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (64 - i2) + offset : (i2 - offset) - i;
            this._mask = i == 64 ? -1L : ((1 << i) - 1) << this._shift;
            this._signShift = (64 - this._shift) - i;
        }

        public long get() {
            int i = (this._mask > (-1L) ? 1 : (this._mask == (-1L) ? 0 : -1));
            long j = Struct.this.getByteBuffer().getLong(position());
            if (i == 0) {
                return j;
            }
            long j2 = j & this._mask;
            int i2 = this._signShift;
            return (j2 << i2) >> (i2 + this._shift);
        }

        public void set(long j) {
            long j2 = this._mask;
            if (j2 == -1) {
                Struct.this.getByteBuffer().putLong(position(), j);
                return;
            }
            Struct.this.getByteBuffer().putLong(position(), ((j << this._shift) & j2) | (Struct.this.getByteBuffer().getLong(position()) & ((-1) ^ this._mask)));
        }
    }

    /* loaded from: classes2.dex */
    public class Signed8 extends Member {
        private final int _mask;
        private final int _shift;
        private final int _signShift;

        public Signed8(Struct struct) {
            this(8);
        }

        public Signed8(int i) {
            super(1, i, 8);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (8 - i2) + offset : (i2 - offset) - i;
            int i3 = this._shift;
            this._mask = ((1 << i) - 1) << i3;
            this._signShift = (32 - i3) - i;
        }

        public byte get() {
            if (this._mask == 255) {
                return Struct.this.getByteBuffer().get(position());
            }
            int i = Struct.this.getByteBuffer().get(position()) & this._mask;
            int i2 = this._signShift;
            return (byte) ((i << i2) >> (i2 + this._shift));
        }

        public void set(byte b) {
            int i = this._mask;
            if (i == 255) {
                Struct.this.getByteBuffer().put(position(), b);
                return;
            }
            Struct.this.getByteBuffer().put(position(), (byte) (((byte) (((byte) (b << this._shift)) & i)) | (Struct.this.getByteBuffer().get(position()) & (this._mask ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class UTF8String extends Member {
        private final char[] _chars;
        private final int _length;
        private final UTF8ByteBufferReader _reader;
        private final UTF8ByteBufferWriter _writer;

        public UTF8String(int i) {
            super(1, i);
            this._writer = new UTF8ByteBufferWriter();
            this._reader = new UTF8ByteBufferReader();
            this._length = i;
            this._chars = new char[i];
        }

        public String get() {
            ByteBuffer byteBuffer = Struct.this.getByteBuffer();
            synchronized (byteBuffer) {
                try {
                    byteBuffer.position(position());
                    this._reader.setInput(byteBuffer);
                    for (int i = 0; i < this._length; i++) {
                        char read = (char) this._reader.read();
                        if (read == 0) {
                            String str = new String(this._chars, 0, i);
                            this._reader.reset();
                            return str;
                        }
                        this._chars[i] = read;
                    }
                    String str2 = new String(this._chars, 0, this._length);
                    this._reader.reset();
                    return str2;
                } catch (IOException e) {
                    throw new JavolutionError(e);
                }
            }
        }

        public void set(String str) {
            UTF8ByteBufferWriter uTF8ByteBufferWriter;
            ByteBuffer byteBuffer = Struct.this.getByteBuffer();
            synchronized (byteBuffer) {
                try {
                    byteBuffer.position(position());
                    this._writer.setOutput(byteBuffer);
                    if (str.length() < this._length) {
                        this._writer.write(str);
                        this._writer.write(0);
                    } else {
                        if (str.length() > this._length) {
                            uTF8ByteBufferWriter = this._writer;
                            str = str.substring(0, this._length);
                        } else {
                            uTF8ByteBufferWriter = this._writer;
                        }
                        uTF8ByteBufferWriter.write(str);
                    }
                    this._writer.reset();
                } catch (IOException e) {
                    throw new JavolutionError(e);
                }
            }
        }
    }

    /* loaded from: classes2.dex */
    public class Unsigned16 extends Member {
        private final int _mask;
        private final int _shift;

        public Unsigned16(Struct struct) {
            this(16);
        }

        public Unsigned16(int i) {
            super(2, i, 16);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (16 - i2) + offset : (i2 - offset) - i;
            this._mask = ((1 << i) - 1) << this._shift;
        }

        public int get() {
            return (Struct.this.getByteBuffer().getShort(position()) & this._mask) >>> this._shift;
        }

        public void set(int i) {
            int i2 = this._mask;
            if (i2 == 65535) {
                Struct.this.getByteBuffer().putShort(position(), (short) i);
                return;
            }
            Struct.this.getByteBuffer().putShort(position(), (short) (((i << this._shift) & i2) | (Struct.this.getByteBuffer().getShort(position()) & (this._mask ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class Unsigned32 extends Member {
        private final long _mask;
        private final int _shift;

        public Unsigned32(Struct struct) {
            this(32);
        }

        public Unsigned32(int i) {
            super(4, i, 32);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (32 - i2) + offset : (i2 - offset) - i;
            this._mask = i == 32 ? InternalZipConstants.ZIP_64_LIMIT : ((1 << i) - 1) << this._shift;
        }

        public long get() {
            return (Struct.this.getByteBuffer().getInt(position()) & this._mask) >>> this._shift;
        }

        public void set(long j) {
            long j2 = this._mask;
            if (j2 == -1) {
                Struct.this.getByteBuffer().putInt(position(), (int) j);
                return;
            }
            Struct.this.getByteBuffer().putInt(position(), (int) (((j << this._shift) & j2) | (Struct.this.getByteBuffer().getInt(position()) & (((int) this._mask) ^ (-1)))));
        }
    }

    /* loaded from: classes2.dex */
    public class Unsigned8 extends Member {
        private final int _mask;
        private final int _shift;

        public Unsigned8(Struct struct) {
            this(8);
        }

        public Unsigned8(int i) {
            super(1, i, 8);
            int offset = offset() << 3;
            ByteOrder byteOrder = Struct.this.byteOrder();
            ByteOrder byteOrder2 = ByteOrder.BIG_ENDIAN;
            int i2 = Struct.this._bitIndex;
            this._shift = byteOrder == byteOrder2 ? (8 - i2) + offset : (i2 - offset) - i;
            this._mask = ((1 << i) - 1) << this._shift;
        }

        public short get() {
            return (short) ((Struct.this.getByteBuffer().get(position()) & this._mask) >>> this._shift);
        }

        public void set(short s) {
            int i = this._mask;
            if (i == 255) {
                Struct.this.getByteBuffer().put(position(), (byte) s);
                return;
            }
            Struct.this.getByteBuffer().put(position(), (byte) (((short) (((short) (s << this._shift)) & i)) | (Struct.this.getByteBuffer().get(position()) & (this._mask ^ (-1)))));
        }
    }

    private synchronized ByteBuffer newBuffer() {
        int i;
        if (this._byteBuffer != null) {
            return this._byteBuffer;
        }
        int size = size();
        if (isPacked() && (i = size & 7) != 0) {
            size = (size + 8) - i;
        }
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(size);
        allocateDirect.order(byteOrder());
        setByteBuffer(allocateDirect, 0);
        return this._byteBuffer;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int updateIndexes(int i, int i2, int i3) {
        int i4;
        if (i2 > i3) {
            throw new IllegalArgumentException("nbrOfBits: " + i2 + " exceeds capacity: " + i3);
        }
        if (this._resetIndex) {
            this._bitIndex = 0;
        }
        if (isPacked()) {
            i = 1;
        }
        int i5 = this._bitIndex;
        int i6 = (i5 / (i << 3)) * i;
        int i7 = i5 - (i6 << 3);
        if (i3 < i7 + i2 || (i2 == 0 && i7 != 0)) {
            i6 += i;
            i4 = i6 << 3;
        } else {
            i4 = this._bitIndex;
        }
        this._bitIndex = i4 + i2;
        int i8 = this._bitsUsed;
        int i9 = this._bitIndex;
        if (i8 < i9) {
            this._bitsUsed = i9;
        }
        if (this._alignment < i) {
            this._alignment = MathLib.min(i, MAXIMUM_ALIGNMENT.get().intValue());
        }
        return i6;
    }

    public final long address() {
        ByteBuffer byteBuffer = getByteBuffer();
        Reflection.Method method = ADDRESS_METHOD;
        if (method != null) {
            return ((Long) method.invoke(byteBuffer)).longValue() + getByteBufferPosition();
        }
        throw new UnsupportedOperationException("Operation not supported for " + byteBuffer.getClass());
    }

    protected <M extends Member> M[] array(M[] mArr) {
        boolean z = this._resetIndex;
        int i = 0;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        if (BOOL.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Bool(this);
                i++;
            }
        } else if (SIGNED_8.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Signed8(this);
                i++;
            }
        } else if (UNSIGNED_8.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Unsigned8(this);
                i++;
            }
        } else if (SIGNED_16.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Signed16(this);
                i++;
            }
        } else if (UNSIGNED_16.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Unsigned16(this);
                i++;
            }
        } else if (SIGNED_32.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Signed32(this);
                i++;
            }
        } else if (UNSIGNED_32.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Unsigned32(this);
                i++;
            }
        } else if (SIGNED_64.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Signed64(this);
                i++;
            }
        } else if (FLOAT_32.isInstance(mArr)) {
            while (i < mArr.length) {
                mArr[i] = new Float32();
                i++;
            }
        } else if (!FLOAT_64.isInstance(mArr)) {
            throw new UnsupportedOperationException("Cannot create member elements, the arrayMember should contain the member instances instead of null");
        } else {
            while (i < mArr.length) {
                mArr[i] = new Float64();
                i++;
            }
        }
        this._resetIndex = z;
        return mArr;
    }

    protected UTF8String[] array(UTF8String[] uTF8StringArr, int i) {
        for (int i2 = 0; i2 < uTF8StringArr.length; i2++) {
            uTF8StringArr[i2] = new UTF8String(i);
        }
        return uTF8StringArr;
    }

    /* JADX WARN: Multi-variable type inference failed */
    protected <S extends Struct> S[] array(S[] sArr) {
        boolean z = this._resetIndex;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        Class cls = null;
        for (int i = 0; i < sArr.length; i++) {
            Object obj = sArr[i];
            if (obj == 0) {
                if (cls == null) {
                    try {
                        String name = sArr.getClass().getName();
                        String substring = name.substring(2, name.length() - 1);
                        Class cls2 = Reflection.getClass(substring);
                        if (cls2 == null) {
                            throw new JavolutionError("Struct class: " + substring + " not found");
                        }
                        cls = cls2;
                    } catch (Exception e) {
                        throw new JavolutionError(e);
                    }
                }
                obj = (Struct) cls.newInstance();
            }
            sArr[i] = inner(obj);
        }
        this._resetIndex = z;
        return sArr;
    }

    protected <M extends Member> M[][] array(M[][] mArr) {
        boolean z = this._resetIndex;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        for (M[] mArr2 : mArr) {
            array(mArr2);
        }
        this._resetIndex = z;
        return mArr;
    }

    protected <S extends Struct> S[][] array(S[][] sArr) {
        boolean z = this._resetIndex;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        for (S[] sArr2 : sArr) {
            array(sArr2);
        }
        this._resetIndex = z;
        return sArr;
    }

    protected <M extends Member> M[][][] array(M[][][] mArr) {
        boolean z = this._resetIndex;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        for (M[][] mArr2 : mArr) {
            array(mArr2);
        }
        this._resetIndex = z;
        return mArr;
    }

    protected <S extends Struct> S[][][] array(S[][][] sArr) {
        boolean z = this._resetIndex;
        if (z) {
            this._bitIndex = 0;
            this._resetIndex = false;
        }
        for (S[][] sArr2 : sArr) {
            array(sArr2);
        }
        this._resetIndex = z;
        return sArr;
    }

    public ByteOrder byteOrder() {
        Struct struct = this._outer;
        return struct != null ? struct.byteOrder() : ByteOrder.BIG_ENDIAN;
    }

    public final ByteBuffer getByteBuffer() {
        Struct struct = this._outer;
        if (struct != null) {
            return struct.getByteBuffer();
        }
        ByteBuffer byteBuffer = this._byteBuffer;
        return byteBuffer != null ? byteBuffer : newBuffer();
    }

    public final int getByteBufferPosition() {
        Struct struct = this._outer;
        return struct != null ? struct.getByteBufferPosition() + this._outerOffset : this._outerOffset;
    }

    protected <S extends Struct> S inner(S s) {
        if (s._outer == null) {
            s._outer = this;
            int size = s.size() << 3;
            updateIndexes(s._alignment, size, size);
            s._outerOffset = (this._bitIndex - size) >> 3;
            return s;
        }
        throw new IllegalArgumentException("struct: Already an inner struct");
    }

    public boolean isPacked() {
        Struct struct = this._outer;
        if (struct != null) {
            return struct.isPacked();
        }
        return false;
    }

    public boolean isUnion() {
        return false;
    }

    public int read(InputStream inputStream) throws IOException {
        int read;
        ByteBuffer byteBuffer = getByteBuffer();
        if (byteBuffer.hasArray()) {
            return inputStream.read(byteBuffer.array(), byteBuffer.arrayOffset() + getByteBufferPosition(), size());
        }
        synchronized (byteBuffer) {
            if (this._bytes == null) {
                this._bytes = new byte[size()];
            }
            read = inputStream.read(this._bytes);
            byteBuffer.position(getByteBufferPosition());
            byteBuffer.put(this._bytes);
        }
        return read;
    }

    public final Struct setByteBuffer(ByteBuffer byteBuffer, int i) {
        if (byteBuffer.order() == byteOrder()) {
            if (this._outer == null) {
                this._byteBuffer = byteBuffer;
                this._outerOffset = i;
                return this;
            }
            throw new UnsupportedOperationException("Inner struct byte buffer is inherited from outer");
        }
        throw new IllegalArgumentException("The byte order of the specified byte buffer is different from this struct byte order");
    }

    public final Struct setByteBufferPosition(int i) {
        return setByteBuffer(getByteBuffer(), i);
    }

    public final int size() {
        int i = (this._bitsUsed + 7) >> 3;
        int i2 = this._alignment;
        return i % i2 == 0 ? i : (i + i2) - (i % i2);
    }

    public String toString() {
        int size = size();
        StringBuffer stringBuffer = new StringBuffer(size * 3);
        ByteBuffer byteBuffer = getByteBuffer();
        int byteBufferPosition = getByteBufferPosition();
        for (int i = 0; i < size; i++) {
            int i2 = byteBuffer.get(byteBufferPosition + i) & 255;
            stringBuffer.append(HEXA[i2 >> 4]);
            stringBuffer.append(HEXA[i2 & 15]);
            stringBuffer.append((i & 15) == 15 ? '\n' : ' ');
        }
        return stringBuffer.toString();
    }

    public void write(OutputStream outputStream) throws IOException {
        ByteBuffer byteBuffer = getByteBuffer();
        if (byteBuffer.hasArray()) {
            outputStream.write(byteBuffer.array(), byteBuffer.arrayOffset() + getByteBufferPosition(), size());
            return;
        }
        synchronized (byteBuffer) {
            if (this._bytes == null) {
                this._bytes = new byte[size()];
            }
            byteBuffer.position(getByteBufferPosition());
            byteBuffer.get(this._bytes);
            outputStream.write(this._bytes);
        }
    }
}
